package com.cmcc.linlaker.ui.upload.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.application.LinhuApp;
import com.cmcc.linlaker.ui.upload.bean.ImageBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

    private List<ImageBean> itemList;
    private Context mContext;

    public ImageAdapter(Context mContext, List<ImageBean> itemList) {
        this.itemList = itemList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.image_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        ImageBean item = itemList.get(position);
        int viewType = getItemViewType(position);
        if (viewType == 0) {
            //添加按钮
            holder.layout_select_pic.setVisibility(View.VISIBLE);
            holder.image.setVisibility(View.GONE);
            holder.layout_delete.setVisibility(View.GONE);

            holder.layout_select_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(position);
                }
            });
        } else {
            holder.layout_select_pic.setVisibility(View.GONE);
            holder.image.setVisibility(View.VISIBLE);
            if (item.bitmap != null) {
                holder.image.setImageBitmap(item.bitmap);
            } else {
                ImageLoader.getInstance()
                        .displayImage(item.path, holder.image, LinhuApp.options);
            }
            holder.layout_delete.setVisibility(View.VISIBLE);

            holder.layout_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemDeleteClick(position);
                }
            });

            holder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(position);
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onItemDeleteClick(int position);
    }

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        return itemList.get(position).type;
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public RelativeLayout layout_delete, layout_select_pic;
        public ImageView image;


        public ViewHolder(View itemView) {
            super(itemView);

            image = itemView.findViewById(R.id.image);
            layout_delete = itemView.findViewById(R.id.layout_delete);
            layout_select_pic = itemView.findViewById(R.id.layout_select_pic);
        }
    }

}

